package com.lb.jwitter.action;


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map; 
import com.opensymphony.xwork2.ActionContext; 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import org.springframework.test.context.ContextConfiguration;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.lb.jwitter.util.ApplicationContextProvider; 
import com.opensymphony.xwork2.Action; 
import com.lb.jwitter.service.ConditionReport;
import com.lb.jwitter.service.TblQLDTTXALopMonHocDuKien;
import com.lb.jwitter.service.TblQLDTTXALopMonHocDuKienService;
import com.lb.jwitter.service.TblQLDTTXALopMonHocHocVien;
import com.lb.jwitter.service.TblQLDTTXALopMonHocHocVienService;
import com.lb.jwitter.service.TblQLDTTXAMonHocBatBuoc;
import com.lb.jwitter.service.TblQLDTTXAMonHocBatBuocService;
import com.lb.jwitter.service.TblQLDTTXAMonHocTienQuyet;
import com.lb.jwitter.service.TblQLDTTXAMonHocTienQuyetService;
import com.lb.jwitter.service.TblQuyenChiTietService;import com.lb.jwitter.service.TblTaiKhoan;import com.lb.jwitter.service.TblQLDTTXAHocVien;
import com.lb.jwitter.service.TblQLDTTXAHocVienService;

import com.lb.jwitter.service.TblCanBo;
import com.lb.jwitter.service.TblCanBoService;

import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNam;
import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNamService;

import com.lb.jwitter.service.TblQLDTTXAHocVienDangKyMonHoc;
import com.lb.jwitter.service.TblQLDTTXAHocVienDangKyMonHocService;

import com.lb.jwitter.service.TblQLDTTXAMonHoc;
import com.lb.jwitter.service.TblQLDTTXAMonHocService;

 /** 
 * @author QUOCVIET  
 * 
 */ 


@ContextConfiguration(locations="classpath:beans.xml") 
public class MonHocDuKienSearchAction{


    private TblQLDTTXAHocKyTrongNamService tblQLDTTXAHocKyTrongNamService; 

    private List<TblQLDTTXAMonHoc> tblQLDTTXAMonHocs; 
    public List<TblQLDTTXAMonHoc>  getTblQLDTTXAMonHocs(){ 
              return tblQLDTTXAMonHocs; 
     } 
    
    private List<TblQLDTTXAMonHoc> tblQLDTTXAMonHocThieuMonTienQuyets; 
    public List<TblQLDTTXAMonHoc>  getTblQLDTTXAMonHocThieuMonTienQuyets(){ 
              return tblQLDTTXAMonHocThieuMonTienQuyets; 
     }
    
    private List<TblQLDTTXAMonHoc> tblQLDTTXAMonHocThieuMonHocTruocs; 
    public List<TblQLDTTXAMonHoc>  getTblQLDTTXAMonHocThieuMonHocTruocs(){ 
              return tblQLDTTXAMonHocThieuMonHocTruocs; 
     }

    private List<TblQLDTTXAHocKyTrongNam> tblQLDTTXAHocKyTrongNams; 
    private TblQLDTTXAMonHocService tblQLDTTXAMonHocService; 
    
    private TblQLDTTXALopMonHocDuKienService tblQLDTTXALopMonHocDuKienService; 
    
    private TblQLDTTXAMonHocBatBuocService tblQLDTTXAMonHocBatBuocService;
    private TblQLDTTXAMonHocTienQuyetService tblQLDTTXAMonHocTienQuyetService;
    private TblQLDTTXALopMonHocHocVienService tblQLDTTXALopMonHocHocVienService;
    
    private int hocKyId;
    private int hocVienId;
    private Date today;

  private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider; 
 public String execute() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 

        tblQLDTTXALopMonHocDuKienService = (TblQLDTTXALopMonHocDuKienService) context.getBean("tbl_QLDTTXA_LopMonHoc_DuKienClient");
        tblQLDTTXAMonHocBatBuocService = (TblQLDTTXAMonHocBatBuocService) context.getBean("tbl_QLDTTXA_MonHocBatBuocClient");
        tblQLDTTXAMonHocTienQuyetService = (TblQLDTTXAMonHocTienQuyetService) context.getBean("tbl_QLDTTXA_MonHocTienQuyetClient");
        tblQLDTTXALopMonHocHocVienService = (TblQLDTTXALopMonHocHocVienService) context.getBean("tbl_QLDTTXA_LopMonHoc_HocVienClient");

        today = new Date();
        List<ConditionReport> cons = new ArrayList<ConditionReport>(); 
        if(hocKyId != 0){ 
            ConditionReport con = new ConditionReport(); 
            con.setCol("hocKyId.id");
            con.setValue1(new Long(hocKyId));
            con.setOperator("eq");
            cons.add(con);	
        }

        List<TblQLDTTXALopMonHocDuKien> tblQLDTTXALopMonHocDuKiens = tblQLDTTXALopMonHocDuKienService.findtblQLDTTXALopMonHocDuKienByCondition(cons, 0, 0);
        
        tblQLDTTXAMonHocs = new ArrayList<TblQLDTTXAMonHoc>();
        tblQLDTTXAMonHocThieuMonTienQuyets = new ArrayList<TblQLDTTXAMonHoc>();
        tblQLDTTXAMonHocThieuMonHocTruocs = new ArrayList<TblQLDTTXAMonHoc>();
        
        List<Long> monHocIds = new ArrayList<Long>();
        for(TblQLDTTXALopMonHocDuKien tblLopDuKien : tblQLDTTXALopMonHocDuKiens){
     	   if(!monHocIds.contains(tblLopDuKien.getMonHocId().getId())){
    		   tblQLDTTXAMonHocs.add(tblLopDuKien.getMonHocId());  
    		   monHocIds.add(tblLopDuKien.getMonHocId().getId());
    	   }
        	
        }
        
        if (hocVienId != 0){
        	for (TblQLDTTXAMonHoc monHoc : tblQLDTTXAMonHocs){
        		cons.clear();
        		ConditionReport con = new ConditionReport(); 
                con.setCol("monHocId.id");
                con.setValue1(monHoc.getId());
                con.setOperator("eq");
                cons.add(con);	
                
                // Kiem tra mon hoc truoc
                List<TblQLDTTXAMonHocBatBuoc> monHocBatBuocs = tblQLDTTXAMonHocBatBuocService.findtblQLDTTXAMonHocBatBuocByCondition(cons, 0, 0);
                for (TblQLDTTXAMonHocBatBuoc monHocBatBuoc : monHocBatBuocs){
                	List<ConditionReport> cons1 = new ArrayList<ConditionReport>(); 
                	
                	ConditionReport con0 = new ConditionReport(); 
                    con0.setCol("hocVienId.id");
                    con0.setValue1(new Long(hocVienId));
                    con0.setOperator("eq");
                    cons1.add(con0);
                    
                	ConditionReport con1 = new ConditionReport(); 
                    con1.setCol("lopMonHocId.monHocId.id");
                    con1.setValue1(monHocBatBuoc.getMonHocBatBuocId().getId());
                    con1.setOperator("eq");
                    cons1.add(con1);
                    
                    // khac trang thai tam dung
                    ConditionReport con2 = new ConditionReport(); 
                    con2.setCol("trangThai");
                    con2.setValue1(new Integer(3));
                    con2.setOperator("ne");
                    cons1.add(con2);
                    
                	int num = tblQLDTTXALopMonHocHocVienService.counttblQLDTTXALopMonHocHocVienByCondition(cons1);
                	if (num == 0){
                		//tblQLDTTXAMonHocs.remove(monHoc);
                		tblQLDTTXAMonHocThieuMonHocTruocs.add(monHoc);
                		break;
                	}
                }
                
                //Kiem tra mon hoc tien quyet
                List<TblQLDTTXAMonHocTienQuyet> monHocTienQuyets = tblQLDTTXAMonHocTienQuyetService.findtblQLDTTXAMonHocTienQuyetByCondition(cons, 0, 0);
                for (TblQLDTTXAMonHocTienQuyet monHocTienQuyet : monHocTienQuyets){
                	List<ConditionReport> cons1 = new ArrayList<ConditionReport>(); 
                	
                	ConditionReport con0 = new ConditionReport(); 
                    con0.setCol("hocVienId.id");
                    con0.setValue1(new Long(hocVienId));
                    con0.setOperator("eq");
                    cons1.add(con0);
                    
                	ConditionReport con1 = new ConditionReport(); 
                    con1.setCol("lopMonHocId.monHocId.id");
                    con1.setValue1(monHocTienQuyet.getMonHocTienQuyetId().getId());
                    con1.setOperator("eq");
                    cons1.add(con1);
                    
                    ConditionReport con2 = new ConditionReport(); 
                    con2.setCol("trangThai");
                    con2.setValue1(new Integer(3));
                    con2.setOperator("ne");
                    cons1.add(con2);
                    
                    ConditionReport con3 = new ConditionReport(); 
                    con3.setCol("tongDiem");
                    con3.setValue1(new Double(5.0));
                    con3.setOperator("geq");
                    cons1.add(con3);
                	int num = tblQLDTTXALopMonHocHocVienService.counttblQLDTTXALopMonHocHocVienByCondition(cons1);
                	if (num == 0){
                		//tblQLDTTXAMonHocs.remove(monHoc);
                		tblQLDTTXAMonHocThieuMonTienQuyets.add(monHoc);
                		break;
                	}
                }
        	}
        	
        }
        	

        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
      System.out.println("Loi MonHocDuKienSearch Action"); 
     e.printStackTrace(); 
    return Action.ERROR; 
 } 
 }

public int getHocKyId() {
	return hocKyId;
}

public void setHocKyId(int hocKyId) {
	this.hocKyId = hocKyId;
}

public Date getToday() {
	return today;
}

public void setToday(Date today) {
	this.today = today;
}
public int getHocVienId(){
	return hocVienId;
}
public void setHocVienId(int hocVienId){
	this.hocVienId = hocVienId;
}

 } 
 
